public class Solution {
    public int findSubstringInWraproundString(String s) {
        int n = s.length();
        int[] dp = new int[n];
        char[] ch = s.toCharArray();

        for (int i = 0; i < n; i++) {
            dp[i] = 1;
        }
        int sum = 0;

        for (int i = 1; i < n; i++) {
            if(ch[i-1] + 1 == ch[i] || (ch[i-1] == 'z' && ch[i] == 'a')){
                dp[i] += dp[i-1];
            }
        }
        int[] hash = new int[26];
        for (int i = 0; i < n; i++) {
            hash[ch[i] - 'a'] = Math.max(hash[ch[i] - 'a'], dp[i]);
        }

        for (int x : hash) {
            sum+=x;
        }
        return sum;

    }
}
